﻿using System;
using System.Collections.Generic;
using System.Linq;
using GraphProcessor;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Logic;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [System.Serializable, NodeMenuItem(LevelEditorConst.DynamicList, typeof(LevelServiceGraph))]
    public class LevelServiceDynamicListSelectNode : LevelServiceBaseNode
    {
        public override Color color => new Color(0.9f, 0.7f, 0.1f, 1);

        public override string name => LevelEditorConst.DynamicList;
        [Input("执行", false)] public EditorLevelServiceDynamicListSetter Setter;

        [LevelServiceEditorCustomProperty("Ta", "EntityID"), Tooltip("和其他参数不能同时使用，自行在实体ID生成时进行配置")]
        public List<int> entityID = new List<int>();
        [LevelServiceEditorCustomProperty("Ta", "动态Key"), Tooltip("和其他参数不能同时使用")]
        public string key;
        [InspectorName("Tag"), Tooltip("tag < 65536,-1为不使用tag,和其他参数不能同时使用"), Range(-1, 65535)]
        public int tag = -1;

        protected override void Process()
        {
            base.Process();
            if (this.Setter == null)
            {
                var rGraphPath = AssetDatabase.GetAssetPath(this.graph);
                LogGame.LogError($"【{rGraphPath}】中节点【{this.name}】未连接原节点");
                return;
            }
            this.Setter.setter?.Invoke(this.CreateConfig());
        }

        protected List<EditorLevelServiceDynamicConfig> CreateConfig()
        {
            var rList = new List<EditorLevelServiceDynamicConfig>();
            if (this.tag >= 0)
            {
                rList.Add(new EditorLevelServiceDynamicConfig()
                {
                    paramType = 0,
                    paramInt = this.tag
                });
                // 使用Tag就忽略其他两种列表参数
                return rList;
            }
            for (int i = 0; i < this.entityID.Count; i++)
            {
                rList.Add(new EditorLevelServiceDynamicConfig()
                {
                    paramType = 0,
                    paramInt = this.entityID[i],
                });
            }
            if(!string.IsNullOrEmpty(this.key))
            {
                rList.Add(new EditorLevelServiceDynamicConfig()
                {
                    paramType = 1,
                    paramStr = this.key,
                });
            }
            return rList;
        }

        public void Sync(List<EditorLevelServiceDynamicConfig> configs)
        {
            this.entityID = configs.Where((x) => x.paramType == 0).Select((x) => x.paramInt).ToList();
            var rList = configs.Where((x) => x.paramType == 1).Select((x) => x.paramStr).ToList();
            if (rList.Count > 0)
            {
                this.key = rList[0];
            }
        }

        private static List<EditorLevelServiceDynamicConfig> _defaultParam;

        public static List<EditorLevelServiceDynamicConfig> DefaultParam
        {
            get
            {
                if (_defaultParam == null)
                {
                    _defaultParam = new List<EditorLevelServiceDynamicConfig>();
                    _defaultParam.Add(new EditorLevelServiceDynamicConfig()
                    {
                        paramType = 0,
                        paramInt = 0,
                    });
                }
                return _defaultParam;
            }
        }
    }

    public class EditorLevelServiceDynamicListSetter
    {
        public Action<List<EditorLevelServiceDynamicConfig>> setter;

        public EditorLevelServiceDynamicListSetter(Action<List<EditorLevelServiceDynamicConfig>> setter)
        {
            this.setter = setter;
        }
    }
}